<?php

namespace Fronts\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class SearchController extends AbstractActionController {

    private $VideosTable;
    private $row_per = 40;

    public function getVideosTable() {
        if (!$this->VideosTable) {
            $pst = $this->getServiceLocator();
            $this->VideosTable = $pst->get("Fronts\Model\VideosTable");
        }
        return $this->VideosTable;
    }
 
    public function searchAction() {
        $this->setlayout();
    }

    private function setlayout() {
        $this->layout('layout/layout');
        $menuWidget = $this->forward()->dispatch('Fronts\Controller\Elements', array(
            'action' => 'menu',
        ));
        $videosnewwidget = $this->forward()->dispatch("Fronts\Controller\Elements", array(
            'action' => 'widgetvideosnews',
        ));
		$linkWidget= $this->forward()->dispatch("Fronts\Controller\Elements", array(
            'action' => 'widgetlink',
        ));
		$this->layout()->setVariable('linkWidget', $linkWidget);
        $this->layout()->setVariable('listcatalog', $menuWidget);
        $this->layout()->setVariable('videosnewwidget', $videosnewwidget);
    }

    public function loadvideosearchAction() {
        $key = addslashes(trim($this->params()->fromPost("keyword")));
        $page = addslashes(trim($this->params()->fromQuery("page")));
        if ($page == null) {
            $page = addslashes(trim($this->params()->fromPost("page")));
        }
        $start = $page;
        $listvideos = $this->getVideosTable()->loadsearch($start, $this->row_per, $key);
        $view = "";
        if (count($listvideos) > 0) {
            foreach ($listvideos as $key => $listdata) {
                $titlevideos = $listdata["videoName"];
                $img = WEB_PATH . "/public/" . $listdata["videoImgThumb"];
                $linkvideos = WEB_PATH . "/" . $listdata["catalogLink"] . "/" . $listdata["videoLink"] . ".html";
                $view .= '<div class="videossub_cate">' .
                        '<a href="' . $linkvideos . '" title="' . $titlevideos . '"><img  class="img-responsive" src="' . $img . '" alt="' . $titlevideos . '"/></a>' .
                        '<div class="captitle">' .
                        '<a href="' . $linkvideos . '" title="' . $titlevideos . '">' . $titlevideos . '</a>' .
                        '</div>' .
                        '</div>';
            }
        } else {
            $view .="Chưa có videos nào với từ khóa này.";
        }
        echo $view;
        die;
    }

    public function loadpagesearchAction() {
        $key = addslashes($this->params()->fromPost("keyword"));
        $pageflag = addslashes(trim($this->params()->fromPost("page")));
        $countpage = ceil($this->getVideosTable()->loadpagesearchmodel($key)) / $this->row_per;
        $view = '<div class="container">
        <div class="ctpage">';

        for ($i = 0; $i < $countpage; $i++) {
            $classdiv = "";
            if ($i == ($pageflag - 1)) {
                $classdiv = "activepage";
            }
            $page = $i + 1;
            $view .= '<a id="page' . $page . '" onclick="pagevideos(' . $page . ');" class="' . $classdiv . '" href="javascript:void(0);" title="Trang ' . $page . '">' . $page . '</a>';
        }
        $view .= '</div></div>*duypc*' . $key;
        echo $view;
        die;
    }

}
